﻿using System;
using System.Collections.Generic;
using Dict.DataModel;
using Dict.Blo;
using Dict.Config;
using ProtoBuf.Message;

namespace UI
{
    public class UIPet
    {
        public TablePet petData;
        public TablePetTalent petTalentData;
        public string guid;
        public string petId;
        public int level;
        public int exp;
        public int breachlevel;//突破等级
        public int intimacy;//亲密度
        public int intimacyLevel;//亲密度等级
        public string initProp;//默认天赋
        public List<string> randPropList = new List<string>();//服务器发过来的随机属性所有的 能解锁的只有一部分 
        public List<PetProp> petPropList;//宠物属性
        public int propCount = 0;//宠物属性个数

        public Dictionary<int, string> talentBranch = new Dictionary<int, string>();

        public Dictionary<string, double> unlockProp = new Dictionary<string, double>();//解锁属性
        public Dictionary<string, double> lockProp = new Dictionary<string, double>();//未解锁属性
        public Dictionary<string, double> addtionProp = new Dictionary<string, double>();//当前等级随机属性

        public Dictionary<string, double> talent = new Dictionary<string, double>();//天赋属性加成
        public Dictionary<string, double> talentGrow = new Dictionary<string, double>();//天赋属性等级成长
        public Dictionary<string, double> talentConstGrow = new Dictionary<string, double>();//天赋属性常量

        public List<PetProp> petProps;
        #region 升级属性
        public BaseAttributeData levelAttribute = new BaseAttributeData (0);
        #endregion
        #region 默认天赋属性
        private BaseAttributeData defaultAttribute;
        #endregion
        #region 基础属性
        private BaseAttributeData baseAttributeData;
        #endregion
        #region 总属性
        private BaseAttributeData currentAttribute;
        #endregion


        public BaseAttributeData unlockAttribute;
        public BaseAttributeData lockAttribute;
        public UIHeroFormation owner;

        public string name
        {
            get
            {
                return TableTextBlo.GetText("name_" + petId);
            }
        }
        public string iconPath
        {
            get
            {
                if (petData != null)
                {
                    return "Pet/" + petData.iconName;
                }
                else
                {
                    return null;
                }
            }
        }
        public void SetPet(string guid)
        {

        }
        public UIPet(string petId)
        {
            this.petId = petId;
            petData = DictPetBlo.GetTablePet(petId);
            if (petData == null)
            {
                throw new System.NullReferenceException("petId:" + petId);
            }
        }


        public void InitData(ProtoBuf.Message.Pet data)
        {
            guid = data.guid;
            exp = data.eat.exp;
            level = data.level;
            breachlevel = data.breached.level;
            intimacyLevel = 0;
            talentBranch.Clear();
            foreach (var i in data.talentList)
            {
                talentBranch.Add(i.talentLevel, i.talent);
            }
            if (data.breached != null)
            {

            }
           // breachAttribute = new BaseAttributeData(0);
            lockAttribute = new BaseAttributeData(0);
            unlockAttribute = new BaseAttributeData(0);
            randPropList = data.randPropList;
            petProps = data.propList;
          
            foreach (var prop in data.propList)
            {
                var temp = new BaseAttributeData(prop.prop);
                if (prop.state == 1)//解锁该属性
                {
                    unlockAttribute.AddProp(temp);
                }
                else
                {
                    lockAttribute.AddProp(temp);
                }
                propCount++;
            }
            this.petPropList = data.propList;
            BaseProp2ShowData();
        }
        public void BaseProp2ShowData()
        {
            for (int i = 0; i < unlockAttribute.attributes.Length; i++)
            {
                var attribute = unlockAttribute.attributes[i];
                if (attribute != 0)
                {
                    string propid = BaseAttributeData.GetDictPropIdByIndex(i);
                    unlockProp[propid] = attribute;
                }
            }
            for (int i = 0; i < lockAttribute.attributes.Length; i++)
            {
                var attribute = lockAttribute.attributes[i];
                if (attribute != 0)
                {
                    string propid = BaseAttributeData.GetDictPropIdByIndex(i);
                    lockProp[propid] = attribute;
                }
            }
        }
        public string GetPetType()
        {
            return "";
        }
        public long GetPetSellPrice()
        {
            long exp = DictPetExpBlo.GetPetExpToLevel(petData.quality, level);
            return exp + this.exp + petData.expBase;
        }

        /// <summary>
        /// 宠物默认属性加成
        /// </summary>
        public BaseAttributeData GetDefaulttAttribute(int petLv,int breathLv)
        {
            petData = DictPetBlo.GetTablePet(petId);
            string[] talentIds = petData.defaultTalent.Split('|');
            if (initProp == "" || initProp == null)//默认情况
                petTalentData = DictPetBreachBlo.GetPetBreachTalent(talentIds[0], breathLv);
            else
                petTalentData = DictPetBreachBlo.GetPetBreachTalent(talentIds[int.Parse(initProp)], breathLv);

            GetTalentAtt(petLv,petTalentData.petTalentId, breathLv);

            var tempBaseProp = new BaseAttributeData(0);
            foreach (var item in talentGrow)
            {
                tempBaseProp.AddProp(item.Key, item.Value);
            }
            tempBaseProp.Multiply(petLv);
            foreach (var item in talent)
            {
                tempBaseProp.AddProp(item.Key, item.Value);
            }
            foreach(var item in talentConstGrow)
            {
                tempBaseProp.AddProp(item.Key, item.Value);
            }
            return tempBaseProp;
        }
        //获取某一条天赋具体属性
        public BaseAttributeData GetTalentAtt(int petLv,string id, int level)
        {
            TablePetTalent petTalent = DictPetBreachBlo.GetPetBreachTalent(id, level);
            Dictionary<string, double> tempTalent = new Dictionary<string, double>();//天赋加成
            Dictionary<string, double> tampTalentConstGrow = new Dictionary<string, double>();//突破常量
            Dictionary<string, double> tampTalentGrow = new Dictionary<string, double>();//天赋属性成长
            string[] petConst = petTalent.talent.Split(';');//突破常量
            string[] petGrow = petTalent.talentGrow.Split(';');//成长
            string[] petGrowConst = petTalent.talentConstGrow.Split(';');//加成
            for (int i = 0; i < petConst.Length; i++)
            {
                var t = petConst[i].Split(',');
                if (t[0] != "")
                {
                    var temp = double.Parse(t[1]);
                    tempTalent[t[0]] = temp;
                }
            }
            for (int i = 0; i < petGrowConst.Length; i++)
            {
                var t = petGrowConst[i].Split(',');
                if (t[0] != "")
                {
                    var temp = double.Parse(t[1]);
                    tampTalentConstGrow[t[0]] = temp;
                }
            }
            for (int i = 0; i < petGrow.Length; i++)
            {
                var t = petGrow[i].Split(',');
                if (t[0] != "")
                {
                    var temp = double.Parse(t[1]);
                    tampTalentGrow[t[0]] = temp;
                }
            }
            var tempBaseProp = new BaseAttributeData(0);
            foreach (var item in talentGrow)
            {
                tempBaseProp.AddProp(item.Key, item.Value);
            }
            tempBaseProp.Multiply(level);
            foreach (var item in talent)
            {
                tempBaseProp.AddProp(item.Key, item.Value);
            }
            foreach (var item in talentConstGrow)
            {
                tempBaseProp.AddProp(item.Key, item.Value);
            }
            return tempBaseProp;
        }
        public BaseAttributeData GetAddtionTalent(int petLv,int breathLV, int quality)
        {
            var addCount = DictPetintimacyBlo.GetAddtionTalentByIntimacyLevelAndQuality(breathLV, quality);
            var totalAddtion = new BaseAttributeData(0);
            for (int i = 0; i < addCount; i++)
            {
                totalAddtion.AddProp(GetTalentAtt(petLv,this.randPropList[i], breathLV));
            }
            return totalAddtion;
        }


        public BaseAttributeData  GetPetBreachLevelAttribute(int breachlevel, int quality)
        {
            var netAtt = new BaseAttributeData();
            //netAtt.Reset();
            //GetAddtionTalent(breachlevel, quality);
            //if(breachlevel >this.breachlevel)
            //{
            //    foreach (var it in talent)
            //    {
            //        netAtt.AddProp(it.Key, it.Value);
            //    }
            //    foreach (var it in talentGrow)
            //    {
            //        netAtt.AddProp(it.Key, it.Value * level);
            //    }
            //    foreach (var it in talentConstGrow)
            //    {
            //        netAtt.AddProp(it.Key, it.Value);
            //    }
            //}else
            //{
            //    //当前属性
            //    netAtt = unlockAttribute;
            //}
            return netAtt;
        }
        #region 数据变更
        /// <summary>
        /// 升级
        /// </summary>
        /// <param name="level"></param>
        public void UpdatePetLevel(int level, int exp)
        {
            this.level = level;
            this.exp = exp;

        }
        /// <summary>
        /// 突破
        /// </summary>
        /// <param name="breachLevel"></param>
        public void UpdatePetBreach(int breachLevel)
        {

        }
        /// <summary>
        /// 互动
        /// </summary>
        /// <param name="intimacyLevel"></param>
        public void UpdatePetintimacy(int intimacyLevel)
        {

        }
        #endregion
        /// <summary>
        /// 宠物升级后属性变化
        /// </summary>
        /// <param name="level"></param>
        public void GetLevelUpBaseProp(int level)
        {
            foreach (var item in talentGrow)
            {
                levelAttribute.AddProp(item.Key, item.Value);
            }
            levelAttribute.Multiply(level);
            foreach (var item in talent)
            {
                levelAttribute.AddProp(item.Key, item.Value);
            }
        }
        /// <summary>
        /// 获取随机具体属性加成
        /// </summary>
        /// <param name="lv"></param>
        /// <param name="quality"></param>
       
        public string GetTalentName()
        {
            string talentName = "";
            foreach (var talent in talentBranch)
            {
                if (breachlevel >= talent.Key)
                {
                    TableTalent talentData = DictTalentBlo.GetTableTalent(talent.Value);
                    if (talentData != null)
                    {
                        talentName = TableTextBlo.GetText(talentData.Name);
                    }
                }
            }
            return talentName;
        }
        public long GetPetProvideExp()
        {
            long exp = DictPetExpBlo.GetPetExpToLevel(petData.quality, level);
            return exp + this.exp + petData.expBase;
        }
    }

}